import Router from 'koa-router';
import { createRecordFile, writeRecordFile } from '../db';
import { Logger, resFail, resSuccess } from '../utils';

const md5: any = require('md5-nodejs');
const router: Router = new Router();

router.post('/', async (ctx) => {
  const { body }: any = ctx.request;
  const { sessionId, recordData } = body;
  try {
    writeRecordFile({ sessionId, recordData });
    ctx.response.body = resSuccess();
  } catch (error) {
    Logger.error(error);
    ctx.response.body = resFail('操作失败');
  }
});

router.get('/start', async (ctx: any) => {
  const sessionId = md5(+new Date());
  try {
    createRecordFile(sessionId);
    ctx.response.body = resSuccess({ sessionId });
  } catch (error) {
    Logger.error(error);
    ctx.response.body = resFail('操作失败');
  }
});

export default router.routes();
